#include "common.h"
#include <main.h>
#include <stdint.h>
#include <cfg_param.h>
#include "sys_log.h"
#include "hal_errorcode.h"

uint16_t Int8ToInt16BigEnd(uint8_t *array) { return ((((uint16_t)array[0]) << 8) + (uint16_t)array[1]); }

uint32_t Int8ToInt32BigEnd(uint8_t *array) { return ((((uint32_t)array[0]) << 24) + (((uint32_t)array[1]) << 16) + (((uint32_t)array[2]) << 8) + ((uint32_t)array[3])); }

void Int16ToInt8BigEnd(uint16_t data, uint8_t *array)
{
    array[0] = (uint8_t)(data >> 8);
    array[1] = (uint8_t)data;
}

void Int32ToInt8BigEnd(uint32_t data, uint8_t *array)
{
    array[0] = (uint8_t)(data >> 24);
    array[1] = (uint8_t)(data >> 16);
    array[2] = (uint8_t)(data >> 8);
    array[3] = (uint8_t)data;
}

// 和校验接口
uint32_t CheckSumCalc(uint8_t *pData, uint32_t len)
{
    uint32_t crcVal = 0;

    while (len != 0)
    {
        --len;
        crcVal += ((uint32_t)*pData);
        ++pData;
    }

    return crcVal;
}

void floatToBytes(float *floatValue, uint8_t *byteArray)
{
    // 使用memcpy将float类型的值拷贝到字节数组中
    memcpy(byteArray, floatValue, sizeof(float));
}

void bytesToFloat(float *floatValue, uint8_t *byteArray)
{
    // 使用memcpy将字节数组中的数据拷贝到float类型变量中
    memcpy(floatValue, byteArray, sizeof(float));
}

// 延时us
void systick_delay_us(uint32_t us)
{
    // // 禁用全局中断
    // __disable_irq();

    uint32_t ticks = us * (SystemCoreClock / 1000000);

    SysTick->LOAD = ticks - 1;
	  SysTick->VAL  = 0;//不设为0的话，打开systick后就会从SysTick->VAL开始递减

    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;

    uint32_t iterations = 0;
	
	 //等待SysTick定时器计数器溢出
    while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk))
    {
        // 添加防止无限循环的保护
        iterations++;

        if (iterations >= SYSTICK_DELAY_WAIT_LIMIT)
        {
            hal_ErrorCodeWrite(ERRORCODE_TYPE_COM_SYSTEM, s_SysTick_Abnormal_Flag, 0, ABNORMAL_STATE);
            break;
        }
    }

    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;

    // // 启用全局中断
    // __enable_irq();
}


// 延时ms
void systick_delay_ms(uint32_t ms)
{
    for (int i = 0; i < ms * 10; i++)
    {
        systick_delay_us(100);
    }
}

// 延时s
void systick_delay_s(uint32_t s)
{
    for (int i = 0; i < s * 10; i++)
    {
        systick_delay_ms(100);
    }
}

void nop_delay_us(uint32_t us) 
{
    // 根据CPU时钟频率计算每个指令的执行时间（以纳秒为单位）
    // 在这里，CPU主频为100MHz，即每个指令执行时间为10纳秒
    const uint32_t instruction_ns = 10;

    // 微秒延时的指令数
    uint32_t delay_count = us * 1000 / instruction_ns;

    // 执行延时
    for (uint32_t i = 0; i < delay_count; ++i) {
        __NOP(); // 空指令，延时一个CPU周期
    }
}
// 定义大小端转换函数接口
void swapEndianFloatAndCopy(float *source)
{
    float temp;
    char *c = (char *)&temp;

    c[0] = ((char *)source)[3];
    c[1] = ((char *)source)[2];
    c[2] = ((char *)source)[1];
    c[3] = ((char *)source)[0];

    memcpy(source, &temp, sizeof(float));
}

void swapEndianUint32AndCopy(uint32_t *dest)
{
    uint32_t source = *dest;
    uint32_t temp;

    char *c = (char *)&temp;

    c[0] = (source >> 24) & 0xFF;
    c[1] = (source >> 16) & 0xFF;
    c[2] = (source >> 8) & 0xFF;
    c[3] = source & 0xFF;

    *dest = temp;
}
